package com.atguigu.gmall.product;

import java.util.concurrent.*;

public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 创建线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5,
                10,
                2, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(20));

        /**
         * 并行代码示例
         */
        // 并行示例代码
        // 创建异步对象：supplyAsync支持返回值。执行返回的结果：hello。
        CompletableFuture futureA = CompletableFuture.supplyAsync(() -> "hello");

        // 获取 futureA 执行的结果
        CompletableFuture futureB = futureA.thenAcceptAsync((t) -> {
            // 调用睡眠的方法
            delaySec(1);
            // 调用打印的方法
            printCurrTime(t + " ===>>> futureB");
        }, threadPoolExecutor);

        // 获取 futureA 执行的结果
        CompletableFuture futureC = futureA.thenAcceptAsync((t) -> {
            // 调用睡眠的方法
            delaySec(3);
            // 调用打印的方法
            printCurrTime(t + " ===>>> futureC");
        }, threadPoolExecutor);

        /**
         * 串行代码示例

         CompletableFuture future = CompletableFuture.supplyAsync(() -> {//  T get();
         return 1024;
         }).thenApply((t) -> {// R apply(T t);
         System.out.println("thenApply 上一个任务返回结果 ===>>> " + t);
         return t * 2;
         }).whenComplete((t, u) -> {//   void accept(T t, U u);
         System.out.println("whenComplete.t ===>>>" + t);
         System.out.println("whenComplete.u ===>>>" + u);
         }).exceptionally((t) -> {
         System.out.println("exceptionally.t ===>>> " + t);
         return 788;
         });
         System.out.println("future.get() ===>>> " + future.get());*/

        /**
         * 计算完成时回调方法示例代码
         // 创建一个 CompletableFuture 对象，有返回值使用 supplyAsync
         CompletableFuture future = CompletableFuture.supplyAsync(new Supplier<Object>() {
        @Override public Object get() {
        System.out.println(Thread.currentThread().getName() + "\t线程");
        int i = 1 / 0;
        return 1024;
        }
        }).whenComplete(new BiConsumer<Object, Throwable>() {// whenComplete 执行当前的线程任务
         @Override public void accept(Object o, Throwable throwable) {
         System.out.println("o ===>>> " + o);
         System.out.println("whenComplete.throwable ===>>> " + throwable);
         }
         }).exceptionally(new Function<Throwable, Object>() {//exceptionally：处理异常情况
         @Override public Object apply(Throwable throwable) {
         System.out.println("exceptionally.throwable ===>>> " + throwable);
         return 788;
         }
         });
         System.out.println("future.get() ===>>> " + future.get());*/
    }

    private static void printCurrTime(String str) {
        System.out.println(str);
    }

    private static void delaySec(int i) {
        try {
            Thread.sleep(i * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

